home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / src / plane / _point.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  2.7 KB  |  123 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _point.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/plane.h>
  16. #include <math.h>
  17. #include <ctype.h>
  18.  
  19. static const double eps = 1e-10;
  20.  
  21.  
  22. //------------------------------------------------------------------------------
  23. // points 
  24. //------------------------------------------------------------------------------
  25.  
  26. point_rep::point_rep()  { count=1; x = y = 0.0; }
  27.  
  28. point_rep::point_rep(double a, double b) 
  29. { x = a; 
  30.   y = b; 
  31.   count = 1; 
  32. }
  33.  
  34.  
  35.  
  36. point::point()                  { ptr = new point_rep; }
  37. point::point(int)               { ptr = new point_rep; }
  38. point::point(ent x)             { ptr= (point_rep*)x; ptr->count++;  }
  39. point::point(double x, double y){ ptr = new point_rep(x,y); }
  40. point::point(vector v)          { ptr = new point_rep(v[0], v[1]); }
  41. point::point(point& p)          { ptr = p.ptr; ptr->count++; }
  42. void point::clear()             { if (--(ptr->count)==0) delete ptr; }
  43.  
  44. point point::rotate(point origin, double alpha)
  45. { if (origin == *this) return *this;
  46.   segment s(origin,*this);
  47.   return s.rotate(alpha).end();
  48. }
  49.  
  50. point point::rotate(double alpha)
  51. { return rotate(point(0,0),alpha);
  52.  }
  53.  
  54. point point::translate(double alpha, double d)
  55. { double dx = cos(alpha) * d;
  56.   double dy = sin(alpha) * d;
  57.   return point(ptr->x+dx,ptr->y+dy);
  58.  }
  59.  
  60. point point::translate(const vector& v)
  61. { return point(ptr->x+v[0],ptr->y+v[1]);
  62.  }
  63.  
  64. double point::distance(point p) 
  65. { return hypot(p.ptr->x - ptr->x, p.ptr->y - ptr->y); } 
  66.  
  67. double point::distance()        
  68. { return distance(point(0,0)); }
  69.  
  70. point& point::operator=(const point& p)
  71. { p.ptr->count++;
  72.   if (--ptr->count == 0)  delete ptr;
  73.   ptr = p.ptr;
  74.   return *this;
  75. }
  76.  
  77. int point::operator==(const point& p) 
  78. { return (fabs(ptr->x - p.ptr->x) < eps && fabs(ptr->y - p.ptr->y) < eps); }
  79.  
  80.  
  81. ostream& operator<<(ostream& out, const point& p)
  82. { out << "(" << p.xcoord() << "," << p.ycoord() << ")"; 
  83.   return out;
  84.  } 
  85.  
  86. istream& operator>>(istream& in, point& p) 
  87. { // syntax: {(} x {,} y {)}
  88.  
  89.   double x,y; 
  90.   char c;
  91.  
  92.   do in.get(c); while (isspace(c));
  93.   if (c != '(') in.putback(c);
  94.  
  95.   in >> x;
  96.  
  97.   do in.get(c); while (isspace(c));
  98.   if (c != ',') in.putback(c);
  99.  
  100.   in >> y; 
  101.  
  102.   do in.get(c); while (c == ' ');
  103.   if (c != ')') in.putback(c);
  104.  
  105.   p = point(x,y); 
  106.   return in; 
  107.  
  108.  } 
  109.  
  110. int compare(point& a, point& b)
  111.   real xa = a.ptr->x;
  112.   real xb = b.ptr->x;
  113.   real ya = a.ptr->y;
  114.   real yb = b.ptr->y;
  115.   int r = compare(xa,xb);
  116.   if (r==0) return compare(ya,yb);
  117.   return r;
  118. }
  119.  
  120.  
  121.